<template>
  <div class="charts-bg">
    <div class="charts-wrapper">
      <!-- 左侧导航栏 -->
      <aside class="charts-nav">
        <div class="nav-title">人气榜单🔥</div>
        <div class="nav-group">
          <ul>
            <li v-for="(item, idx) in workNav" :key="item.key" :class="{active: activeWorkNav === idx}" @click="selectWorkNav(idx)">
              {{ item.label }}
            </li>
          </ul>
        </div>
      </aside>
      <!-- 右侧榜单内容 -->
      <main class="charts-main">
        <div class="main-header">
          <div class="main-title">{{ currentTitle }}</div>
          <div class="main-date">更新时间：{{Dq_shijian}}</div>
        </div>
        <div class="main-list">
          <div v-for="(book, idx) in currentList" :key="book.id" class="book-item">
            <div class="rank-badge" :class="['rank-'+(idx+1)]">
              <span v-if="idx < 3">{{ idx+1 }}</span>
              <span v-else>{{ (idx+1).toString().padStart(2, '0') }}</span>
            </div>
            <img :src="book.image" class="book-cover" :alt="book.title">
            <div class="book-info">
              <div class="book-title-row">
                <span class="book-title">{{ book.title }}</span>
                <span class="book-author">{{ book.author }}</span>
              </div>
              <div class="book-meta">{{ book.sort[0] }} · {{ book.over }}</div>
              <div class="book-desc">{{ book.content }}</div>
              <div class="book-update">最新章节：{{ book.new }} <span class="book-update-time">{{ book.time }}</span></div>
            </div>
            <div class="book-ops">
              <div class="book-hot">{{ book.zishu }}字</div>
              <button class="detail-btn" @click="BooskDetails(book)">作品详情</button>
              <button class="add-btn"  @click="">加入收藏</button>
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      workNav: [
        { key: 'month', label: '阅读榜' },
        { key: 'sale', label: '24小时畅销榜' },
        { key: 'new', label: '新锐榜' },
        { key: 'recommend', label: '推荐榜' },
        { key: 'finish', label: '完结榜' },
        { key: 'update', label: '24小时更新榜' }
      ],
      authorNav: [
        { key: 'author-hot', label: '作者人气榜' },
        { key: 'author-month', label: '月票冠军·新星作品' }
      ],
      activeWorkNav: 0,
      activeAuthorNav: -1,
      // 示例数据，实际可替换为接口数据
      bookLists: {
      },
      //当前时间
      Dq_shijian:''
    }
  },
  computed: {
    currentList() {
      if (this.activeAuthorNav !== -1) {
        return this.bookLists || [];
      }
      return this.bookLists || [];
    },
    currentTitle() {
      if (this.activeAuthorNav !== -1) {
        return this.authorNav[this.activeAuthorNav].label;
      }
      return this.workNav[this.activeWorkNav].label;
    }
  },
  mounted() {
    this.initRankData(this.activeWorkNav + 1);
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth() + 1; // 月份是从 0 开始计数的，因此要加1
    this.Dq_shijian = `${year}-${month}`
  },
  methods: {
    // 初始化榜单数据
    initRankData(activeWorkNavState) {
      axios.post('/api/index/rank',{id:activeWorkNavState})
      .then(response => {
        console.log('获取榜单数据成功:', response.data.zhi);
        this.bookLists = response.data.zhi;
      }).catch(error => {
        console.error('获取榜单数据失败:', error);
      });
    },
    //点击分类
    selectWorkNav(idx) {
      this.activeWorkNav = idx;
      this.activeAuthorNav = -1;
      this.initRankData(idx+1);
     
    },
    selectAuthorNav(idx) {
      this.activeAuthorNav = idx;
      this.activeWorkNav = -1;
    },
    // 作品详情
    BooskDetails(book) {
      this.$router.push(`/BookDetails/${book.id}`);
    }
  }
}
</script>

<style lang="scss" scoped>
.charts-bg {
  width: 100%;
  min-height: 100vh;
  background: #f7f8fa;
  padding: 40px 0;
}
.charts-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  gap: 32px;
}
.charts-nav {
  width: 220px;
  height: 300px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 32px 0 32px 0;
  display: flex;
  flex-direction: column;
  .nav-title {
    font-size: 18px;
    font-weight: bold;
    color: #e94f4f;
    margin-bottom: 18px;
    letter-spacing: 1px;
    margin-left: 30px;
  }
  .nav-group {
    margin-bottom: 24px;
    .group-title {
      font-size: 15px;
      color: #888;
      margin-left: 30px;
      margin-bottom: 8px;
    }
    ul {
      list-style: none;
      padding: 0 0 0 15px;
      margin: 0;
      li {
        font-size: 16px;
        color: #444;
        padding: 8px 0 8px 15px;
        cursor: pointer;
        border-radius: 6px 0 0 6px;
        margin-bottom: 2px;
        transition: background 0.2s, color 0.2s;
        
        &.active {
          background: linear-gradient(90deg, #ffeaea 60%, #fff 100%);
          color: #e94f4f;
          font-weight: bold;
          
        }
        &:hover {
          background: linear-gradient(90deg, #ffeaea 60%, #fff 100%);
          color: #e94f4f;
        }
      }
    }
  }
}
.charts-main {
  flex: 1;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 32px 40px 40px 40px;
  min-width: 0;
  .main-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 24px;
    margin-bottom: 24px;
    .main-title {
      font-size: 22px;
      font-weight: bold;
      color: #e94f4f;
      letter-spacing: 2px;
    }
    .main-date {
      font-size: 15px;
      color: #888;
      margin-left: 8px;
    }
    .main-rule {
      font-size: 14px;
      color: #b63228;
      margin-left: auto;
      cursor: pointer;
      transition: color 0.2s;
      &:hover {
        color: #e94f4f;
      }
    }
  }
  .main-list {
    display: flex;
    flex-direction: column;
    gap: 18px;
    .book-item {
      display: flex;
      align-items:center;
      background: #fff;
      border-radius: 10px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.03);
      padding: 18px 18px;
      position: relative;
      transition: box-shadow 0.2s;
      &:hover {
        box-shadow: 0 4px 16px rgba(233,79,79,0.08);
      }
      .rank-badge {
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 13px;
        font-weight: bold;
        margin-right: 18px;
        border-radius: 50%;
        background: #f7f8fa;
        color: #bcbcbc;
        &.rank-1 {
          background: linear-gradient(135deg, #ffe7b2, #ffd36e);
          color: #e6941e;
        }
        &.rank-2 {
          background: linear-gradient(135deg, #e3e8f7, #b6c6f7);
          color: #4e7be6;
        }
        &.rank-3 {
          background: linear-gradient(135deg, #f7e3e3, #f7b6b6);
          color: #e94f4f;
        }
      }
      .book-cover {
        width: 100px;
        height: 120px;
        object-fit: cover;
        border-radius: 6px;
        margin-right: 18px;
        box-shadow: 0 2px 8px rgb(103, 103, 103);
        background: #f2f2f2;
      }
      .book-info {
        flex: 1;
        min-width: 0;
        .book-title-row {
          display: flex;
          align-items: center;
          gap: 12px;
          margin-bottom: 2px;
          .book-title {
            font-size: 18px;
            font-weight: bold;
            color: #222;
            margin-right: 8px;
            max-width: 220px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .book-author {
            font-size: 14px;
            color: #b63228;
            margin-left: 2px;
          }
        }
        .book-meta {
          font-size: 13px;
          color: #888;
          margin-bottom: 6px;
        }
        .book-desc {
          font-size: 14px;
          color: #444;
          margin-bottom: 6px;
          line-height: 1.5;
          max-width: 600px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .book-update {
          font-size: 13px;
          color: #888;
          margin-bottom: 0;
          .book-update-time {
            margin-left: 8px;
            color: #bbb;
          }
        }
      }
      .book-ops {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        min-width: 90px;
        margin-left: 18px;
        .book-hot {
          font-size: 18px;
          color: #e94f4f;
          font-weight: bold;
          margin-bottom: 10px;
        }
        .detail-btn {
          background: #e94f4f;
          color: #fff;
          border: none;
          border-radius: 20px;
          padding: 6px 18px;
          font-size: 14px;
          margin-bottom: 8px;
          cursor: pointer;
          transition: background 0.2s;
          &:hover {
            background: #b63228;
          }
        }
        .add-btn {
          background: #fff;
          color: #e94f4f;
          border: 1px solid #e94f4f;
          border-radius: 20px;
          padding: 6px 18px;
          font-size: 14px;
          cursor: pointer;
          transition: background 0.2s, color 0.2s;
          &:hover {
            background: #ffeaea;
            color: #b63228;
          }
        }
      }
    }
  }
}
@media (max-width: 900px) {
  .charts-wrapper {
    flex-direction: column;
    gap: 0;
  }
  .charts-nav {
    width: 100%;
    flex-direction: row;
    padding: 0;
    border-radius: 14px 14px 0 0;
    .nav-group {
      margin-bottom: 0;
      ul {
        padding-left: 12px;
      }
    }
  }
  .charts-main {
    padding: 24px 10px 24px 10px;
  }
}
</style>
